package com.sc.nft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sc.nft.dao.FilterIncomeListDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.entity.SmsCodeRecord;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.CheckParameterService;
import com.sc.nft.sup.ErrorCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.regex.Pattern;

@Service
public class CheckParameterServiceImpl implements CheckParameterService {

    @Resource
    private UserInfoDao userInfoDao;



    @Override
    public void CheckVerificationcode(String userAccount, Long smsId, String codes, Integer types) {


        SmsCodeRecord selectPram = new SmsCodeRecord();
        selectPram.setId(smsId);
        SmsCodeRecord smsCodeRecord = (SmsCodeRecord)selectPram.selectById();
        if(ObjectUtil.isEmpty(smsCodeRecord)){
            throw new GlobalRunTimeException(ErrorCode.VERIFICATION_ERROR);
        }

        Date createTime = smsCodeRecord.getCreateTime();
        Integer type = smsCodeRecord.getType();
        String codes1 = smsCodeRecord.getCodes();
        String userTel = smsCodeRecord.getUserTel();

        if (type != types || !userTel.equals(userAccount)) {
            throw new GlobalRunTimeException(ErrorCode.ACCOUNT_IS_INCORRECT);
        }

        long timeOut = (new Date().getTime() - createTime.getTime()) / 1000;
        if (timeOut > 300) {
            throw new GlobalRunTimeException(ErrorCode.CAPTCHA_EXPIRE);
        }
        if (!codes1.equals(codes)) {
            throw new GlobalRunTimeException(ErrorCode.VERIFICATION_ERROR);
        }

    }

    @Override
    public void registeUser(String pwd) {
        if(!isPwd(pwd)){
            throw new GlobalRunTimeException(ErrorCode.PASSWORD_FORMAT_ERROR);
        }

    }



    private boolean isPwd(String str) {
        String reg = "^[a-zA-Z]\\w{6,16}$";
        if (StringUtils.isBlank(str) || StringUtils.isBlank(reg)) {
            return false;
        }
        return Pattern.compile(reg).matcher(str).matches();
    }
}
